﻿using Npgsql;
using System;
using System.Data;
using System.Xml.Linq;
using SqlClient = System.Data.SqlClient;

namespace DSSDataServiceGenerator.Model
{
    class SqlConnection
        : XObject,
            IConnection
    {
        public SqlConnection()
        {
            this.Freeze();

            //this.ConnectionString = "Server=150.151.250.17;Port=5432;User Id=User1;Password=abc123;Database=postgres;enlist=true";

            this._Connection = new SqlClient.SqlConnection();

            this.Server = ".\\SQLEXP11";

            this.Port = 1433;

            this.Username = "sa";

            this.Password = "123";

            this.Database = "master";

            this.Enlist = true;

            this.Release();
        }

        public SqlClient.SqlConnection _Connection = null;
        public IDbConnection Connection
        {
            get
            {
                if (this._Connection == null)

                    lock (this._Connection)

                        this._Connection = new SqlClient.SqlConnection(string.Concat("Server=", this.Server, "; Port=", this.Port, "; User Id=", this.Username, "; Password=", this.Password, "; Database=", this.Database, "; enlist=", this.Enlist));

                return this._Connection;
            }
        }

        protected override void OnPropertyChanged()
        {
            this._Connection = null;

            base.OnPropertyChanged();
        }

        public string ConnectionType
        {
            get { return "Microsoft SQL Server"; }
        }

        private string _Server;

        public string Server
        {
            get { return _Server; }
            set
            {
                _Server = value;

                base.OnPropertyChanged("Server");
            }
        }

        private int _Port;

        public int Port
        {
            get { return _Port; }
            set
            {
                _Port = value;

                base.OnPropertyChanged("Port");
            }
        }

        private string _Username;

        public string Username
        {
            get { return _Username; }
            set
            {
                _Username = value;

                base.OnPropertyChanged("Username");
            }
        }

        private string _Password;

        public string Password
        {
            get { return _Password; }
            set
            {
                _Password = value;

                base.OnPropertyChanged("Password");
            }
        }

        private string _Database;

        public string Database
        {
            get { return _Database; }
            set
            {
                _Database = value;

                base.OnPropertyChanged("Database");
            }
        }

        private bool _Enlist;

        public bool Enlist
        {
            get { return _Enlist; }
            set
            {
                _Enlist = value;

                base.OnPropertyChanged("Enlist");
            }
        }


        public XElement GenerateMetadata()
        {
            return null;
        }
    }
}
